<template>
  <div>
    <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
      <el-form-item label="作业名称" prop="jobName">
        <el-input v-model="formData.jobName" placeholder="请输入作业名称" :disabled='true' clearable
                  :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="启动方式" prop="startModel">
        <el-radio-group v-model="formData.startModel" size="medium" @change="radioChange">
          <el-radio v-for="(item, index) in startModelOptions" :key="index" :label="item.value"
                    :disabled="item.disabled">{{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="保存点选项" prop="savepointId" v-if="formData.startModel==='savepoint'">
        <el-select v-model="formData.savepointId" placeholder="请选择保存点选项" clearable :style="{width: '100%'}"
                   @change="savePointChange">
          <el-option v-for="item in formData.savePointList" :key='item.id' :label='item.createTime'
                     :value='item.id'></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="恢复点地址" prop="startPath" v-if="formData.startModel!=='normal'">
        <el-input v-model="formData.startPath" placeholder="请输入恢复点地址" readonly clearable
                  :style="{width: '100%'}"></el-input>
      </el-form-item>

      <el-form-item label-width="175px" label="allowNonRestoredState" v-if="formData.startModel!=='normal'">
        <el-switch v-model="formData.allowNonRestoredState"></el-switch>
      </el-form-item>

      <el-form-item size="mini">
        <el-button type="primary" @click="submitForm">启动作业</el-button>
        <el-button @click="resetForm">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {getCheckPointByJobId, getSavePointByJobId} from "@/api/resource/savepoint";
import {startJob} from "@/api/resource/job";
import Swal from "sweetalert2";
import {stopJob} from "@/api/resource/job";

export default {
  components: {},
  props: {
    formData: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      currentSavePointList: [],
      rules: {
        jobName: [],
        startModel: [{
          required: true,
          message: '启动方式不能为空',
          trigger: 'change'
        }],
        savepointId: [{
          required: true,
          message: '保存点启动时不能为空',
          trigger: 'change'
        }],
        startPath: [{
          required: true,
          message: '请输入恢复点地址',
          trigger: 'blur'
        }],
      },
      startModelOptions: [{
        "label": "最新检查点",
        "value": "checkpoint"
      }, {
        "label": "保存点",
        "value": "savepoint"
      }, {
        "label": "直接启动",
        "value": "normal"
      }]
    }
  },
  computed: {},
  watch: {},
  created() {
  },
  mounted() {
  },
  methods: {
    submitForm() {
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        let param;
        if (this.formData.startModel === 'normal') {
          param = {"jobId": this.formData.jobId}
        } else {
          param = {
            "jobId": this.formData.jobId,
            "startFromSavePoint": {
              "savePointDir": this.formData.startPath,
              "allowNonRestoredState": this.formData.allowNonRestoredState
            }
          }
        }
        Swal.fire({
          icon: 'success',
          title: 'The current job is startting',
          showConfirmButton: false,
          timer: 2000
        }).then((r) => {
          startJob(param).then(res => {
            this.$emit("startDialogCloseEvent", true)
          })
        })
      })
    },
    radioChange(v) {
      this.formData.startPath = '';
      this.formData.savePointList = [];
      this.formData.savepointId = '';
      if (v === 'checkpoint') {
        getCheckPointByJobId(this.formData.jobId).then(res => {
          if (res.data != null) {
            this.formData.startPath = res.data.path
          }
        })
      }
      if (v === 'savepoint') {
        getSavePointByJobId(this.formData.jobId).then(res => {
          if (res.rows != null) {
            this.formData.savePointList = res.rows
            this.currentSavePointList = res.rows;
          }
        })
      }
    },
    savePointChange(v) {
      for (let i = 0; i < this.currentSavePointList.length; i++) {
        if (this.currentSavePointList[i].id === v) {
          this.formData.startPath = this.currentSavePointList[i].path;
          return;
        }
      }
    },
    resetForm() {
      this.$emit("startDialogCloseEvent", false)
    },
  }
}

</script>
<style>
</style>
